package controller.admin;

import com.jfoenix.controls.JFXComboBox;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import sql.JDBC_T;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Vector;

public class AdminSelectCourseClassController {
    @FXML
    private JFXComboBox adm_chzcourse_combox_classid;
    @FXML
    private JFXComboBox adm_chzcourse_combox_cid;

    public void initialize() throws SQLException {
        JDBC_T jdbc_t = new JDBC_T();
        if (jdbc_t.is_conn()) {
            String sql = "select CID from class order by CID";
            PreparedStatement preparedStatement = jdbc_t.getConn().prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            ObservableList<String> classid, courseid;
            classid = FXCollections.observableArrayList();
            courseid = FXCollections.observableArrayList();
            while (resultSet.next()) {
                classid.add(resultSet.getString("CID"));
            }
            sql = "select CID from course order by CID";
            PreparedStatement preparedStatement1 = jdbc_t.getConn().prepareStatement(sql);
            ResultSet resultSet1 = preparedStatement1.executeQuery(sql);
            while (resultSet1.next()) {
                courseid.add(resultSet1.getString("CID"));
            }
            resultSet1.close();
            preparedStatement1.close();
            resultSet.close();
            preparedStatement.close();
            adm_chzcourse_combox_cid.setItems(courseid);
            adm_chzcourse_combox_classid.setItems(classid);
        }


    }

    public void classsel() {
        Alert alert = new Alert(Alert.AlertType.WARNING);
        String classid = "";
        String courseid = "";
        try {
            classid = (String) adm_chzcourse_combox_classid.getValue();
            courseid = (String) adm_chzcourse_combox_cid.getValue();
            if(classid.equals("")||courseid.equals(""));

        } catch (NullPointerException e) {
            alert.setContentText("请选择好班级以及课程号!!!");
            alert.showAndWait();
            return;
        }


        String sql = MessageFormat.format("INSERT INTO cscore (cscore.SID,cscore.CID)\n" +
                "\tSELECT stuinfo.SID,course.CID\n" +
                "\tFROM stuinfo,course\n" +
                "\tWHERE stuinfo.Class = {0}{1}{0} and course.CID = {0}{2}{0};", "'", classid, courseid);
        //执行
        System.out.println(sql);

}

}
